package com.idrl.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;


import com.idrl.dao.po.MonthInfo;
import com.idrl.Const;
import com.idrl.dao.po.*;

public class TopicAction extends BaseAction {

	private static Logger log = Logger.getLogger(TopicAction.class);	
		
	/**
	 * 站长的贴
	 */
	private static Topic myTopic = null;
	
	private Date currDate;
	
	private Topic topic;
	
	private User user;
	
	private List<Topic> topicList;

	/** 月历 */
	private MonthInfo monthInfo;	
	
	/**
	 * wap显示主页
	 * @return
	 */
	public String execute()
	{
		return this.processTopicList(8, 8);
	}
	
	/**
	 * wap显示单条信息
	 * @return
	 */
	public String show()
	{
		topic = this.topicService.get(topic.getId());
		user = topic.getOwner();
		return INFO;
	}
	
	/**
	 * wap显示默认的条数
	 */
	public String list()
	{
		
		if ( user == null || user.getId() == null)
		{
			user = this.getUserFromSession();
		}
		else
		{
			user = this.userService.get(user.getId());
		}
		
		topicList = this.topicService.getTopicList(user.getId(), Const.DEFAULT_SIZE);
		this.abbreviate(topicList, 8);
		return LIST;
	}
	
	/**
	 * web显示主页
	 * @return
	 */
	public String showHome()
	{
		return this.processTopicList(15, 35);
	}	
	
	
	/**
	 * web按天显示
	 * @return
	 */
	public String showDay()
	{
		if(currDate == null)
		{
			currDate = new Date();
		}
		

		if ( user == null || user.getId() == null)
		{
			user = this.getUserFromSession();
		}
		else
		{
			user = this.userService.get(user.getId());
		}
		
		topicList = this.topicService.getDayList(
				user.getId(),
				currDate);

		
		return "daylist";
	}
	
	/**
	 * web按月显示。
	 * @return
	 */
	public String showMonth()
	{
		if ( monthInfo == null )
		{
			monthInfo = new MonthInfo();
		}
		

		if ( user == null || user.getId() == null)
		{
			user = this.getUserFromSession();
		}
		else
		{
			user = this.userService.get(user.getId());
		}
		
		topicList = this.topicService.getMonthList(
				user.getId(),
				monthInfo.getCurrDate());
		
         //日历
		{
			String preStr = getOwner();
			DayInfo dayInfo = null;
			Calendar cal = Calendar.getInstance();
		    StringBuffer title = null;
		    int dayNo ;
		    for(Topic topic : topicList )
		    {	    	
		    	cal.setTime(topic.getPubdate());
		    	dayNo = cal.get(Calendar.DAY_OF_MONTH);
		    	dayInfo = monthInfo.getDayList().get(monthInfo.getFirstIndex()+ dayNo - 1);
		    	
		    	title = new StringBuffer(preStr);
		    	title.append("于").append(dayNo).append("号");		    	
		    		    	
		    	title.append("发表了").append(StringUtils.abbreviate( topic.getContent(), 15)).append("\n");		    	
		    	dayInfo.setTitle(title.append(StringUtils.trimToEmpty(dayInfo.getTitle())).toString());		    	
		    	
		    	dayInfo.setRemark("&dateStr=" + DateFormatUtils.format(topic.getPubdate(), "yyyy-MM-dd")
		    			+ "&user.id=" + user.getId());
		    }
		}
		
		return "monthlist";
	}
	
	
	
	/**
	 * 转向留言页面。
	 * @return
	 */
	public String gotoAdd()
	{
		return INPUT;
	}
	
	/**
	 * 发表主题。
	 * @return
	 */
	public String addTitle()
	{
		topic.setOwner(this.getUserFromSession());
		topic.setReciever(this.getUserFromSession());
		topic.setPubdate(new Date());
		topic.setState(0);
		topic.setContent(topic.getContent().replaceAll("\n", "<br/>"));
		this.topicService.save(topic);
		this.getRequest().setAttribute(Const.KEY_INFO_MSG, "发表成功!");
		return MSGINFO;
	}

	private void abbreviate(List<Topic> list, int size)
	{
		if(list != null)
		{
			for(Topic tmp : list)
			{
				tmp.setContent(StringUtils.abbreviate( tmp.getContent().replaceAll("<br/>", "\n"), size));
			}
		}
	}

	/**
	 * 
	 * @return
	 */
	private String processTopicList(int recordNum, int wordNum)
	{
		topicList = this.topicService.getTopicList(recordNum);
		this.abbreviate(topicList, wordNum);
//		for(int i = 0; i < topicList.size(); i++)
//		{
//			if(topicList.get(i).getId().longValue() 
//					== this.getMyTopic().getId())
//			{
//				topicList.remove(i);
//				break;
//			}
//		}
//
//		topicList.add(0, this.getMyTopic());
		
		return HOME;
	}

	
	public Topic getMyTopic() {
		if(myTopic == null)
		{
			List<Topic> tmp = this.topicService.getTopicList(1L, 1); 
			if (tmp != null && tmp.size() > 0)
			{
				myTopic = tmp.get(0);
			}
		}
		return myTopic;
	}



	public void setMyTopic(Topic myTopic) {
		TopicAction.myTopic = myTopic;
	}


	public void setCurrDate(Date currDate) {
		this.currDate = currDate;
	}

	public Date getCurrDate() {
		return currDate;
	}


	public MonthInfo getMonthInfo() {
		return monthInfo;
	}


	public void setMonthInfo(MonthInfo monthInfo) {
		this.monthInfo = monthInfo;
	}


	public Topic getTopic() {
		return topic;
	}


	public void setTopic(Topic topic) {
		this.topic = topic;
	}


	public List<Topic> getTopicList() {
		return topicList;
	}


	public void setTopicList(List<Topic> topicList) {
		this.topicList = topicList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getOwner()
	{
		if(user != null && user.getId() != null)
		{
			if (this.getUserFromSession() != null 
					&&	user.getId().longValue() == this.getUserFromSession().getId())
			{
				return "我";
			}
			else
			{
				return user.getNickName();
			}
		}
		else
		{
			log.error("user is null!");
			return "";
		}
	}

	public void setDateStr(String dateStr) {
		SimpleDateFormat  df = new SimpleDateFormat ("yyyy-MM-dd");
		try {
			this.currDate = df.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
